package com.cff.springbootwork.caffeine.controller;

import com.cff.springbootwork.caffeine.enity.UserInfo;
import com.cff.springbootwork.caffeine.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/caffeine")
@Slf4j
public class CaffeineRest {

    @Autowired
    UserInfoService userInfoService;


    @RequestMapping(value = "/test", method = {RequestMethod.GET})
    @Cacheable(value = "cache1", key = "#root.targetClass.simpleName+'-'+#root.methodName+'-'+#userName")
    public UserInfo getUserInfoByUserName(@RequestParam("userName") String userName) {
        return userInfoService.getUserInfoByUserName(userName);
    }

    @RequestMapping(value = "/test1", method = {RequestMethod.GET})
    @Cacheable(value = "cache1")
    public String getNameById(int id) {
        log.info("从DB获取数据：id=" + id);
        return new Date().toString();
    }


}
